<template>
  <div class="home-container">
    <!-- navbar -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        class="search-btn"
        slot="title"
        icon="search"
        round
        type="info"
        size="small"
        to="/search"
      >
        搜索</van-button
      >
    </van-nav-bar>

    <!-- 文章频道 -->
    <van-tabs class="channel-tabs" v-model="active">
      <van-tab
        class="channel-tabs-item"
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name"
        ><articleList :channel="channel"></articleList
      ></van-tab>
      <div slot="nav-right" class="space-occupation"></div>
      <div slot="nav-right">
        <van-icon name="wap-nav" @click="show = true" class="wap-nav-wrap" />
      </div>
    </van-tabs>

    <!-- 弹出层 -->
    <van-popup
      v-model="show"
      closeable
      position="bottom"
      close-icon-position="top-left"
      style="height: 100%"
      get-container="body"
      ><channelEdit
        :active="active"
        :user-channels="channels"
        @delete="active = $event"
        @close="close1"
      ></channelEdit
    ></van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import articleList from '@/views/home/components/article-list'
import channelEdit from '@/views/home/components/channel-edit'
import { mapState } from 'vuex'
export default {
  name: 'home',
  components: { articleList, channelEdit },
  data () {
    return {
      active: 0,
      channels: [],
      show: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      let channels = []
      if (this.user) {
        const res = await getUserChannels()
        channels = res.data.data.channels
      } else {
        const localChannels = JSON.parse(
          window.localStorage.getItem('user-channels')
        )
        // console.log(localChannels);
        if (localChannels) {
          channels = localChannels
        } else {
          const res = await getUserChannels()
          channels = res.data.data.channels
        }
      }
      // const res = await getUserChannels();
      this.channels = channels
    },
    close1 (index) {
      this.active = index
      this.show = false
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  .app-nav-bar {
    /deep/ .van-nav-bar__title {
      max-width: unset;
      .search-btn {
        width: 277px;
        height: 32px;
        background-color: #5babfb;
        border: none;
        .van-icon {
          font-size: 16px;
        }
        .van-button__text {
          font-size: 14px;
        }
      }
    }
  }

  .wap-nav-wrap {
    position: fixed;
    right: 0;
    height: 44px;
    line-height: 44px;
    font-size: 29px;
    background-color: #fff;
    opacity: 0.9;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
/deep/ .van-tabs__line {
  width: 15px !important;
  height: 3px;
  background-color: #3296fa;
  bottom: 20px;
}
.space-occupation {
  width: 24px;
  flex-shrink: 0;
}
</style>
